package com.hs.srm.admin.modules.requisition.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.hs.srm.admin.modules.pricemanagement.vo.EssentialInformationVO;
import com.hs.srm.admin.modules.requisition.dao.ProPurchRequestItemDao;
import com.hs.srm.admin.modules.requisition.entity.ProPurchRequestItemEntity;
import com.hs.srm.admin.modules.requisition.service.ProPurchRequestItemService;
import com.hs.srm.admin.modules.ssess.vo.SsessAndProPurchRequest;
import com.hs.srm.base.common.annotation.DataFilter;
import com.hs.srm.base.common.utils.QueryParam;
import com.hs.srm.common.utils.PageUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author yanfei
 * @date 2019年6月15日
 */
@Service("proPurchRequestItemService")
public class ProPurchRequestItemServiceImpl extends ServiceImpl<ProPurchRequestItemDao, ProPurchRequestItemEntity> implements ProPurchRequestItemService {


    //注入DAO
    @Autowired
    private ProPurchRequestItemDao proPurchRequestItemDao;

    @Override
    @DataFilter
    public PageUtils queryPage(Map<String, Object> params) {
        // 从列表页面获取对应的值
        String purchRequestCode = (String) params.get("purchRequestCode");//采购申请单号
        String requestDateStart = (String) params.get("requestDateStart");//需求日期(起)
        String requestDateEnd = (String) params.get("requestDateEnd");//交期（止）
        String purchGroup = (String) params.get("purchGroup");//采购组
        String goodsStatus = (String) params.get("goodsStatus");
        String factory = (String) params.get("factory");
        String materialCode = (String) params.get("materialCode");//物料号
        String vendorCode = (String) params.get("vendorCode");//供应商代码
        String applier = (String) params.get("applier");//申请人
        Page<ProPurchRequestItemEntity> page = this.selectPage(
                new QueryParam<ProPurchRequestItemEntity>(params).getPage(),
                new EntityWrapper<ProPurchRequestItemEntity>()
                        .eq(StringUtils.isNotBlank(purchRequestCode), "purch_request_code", purchRequestCode)
                        .ge(StringUtils.isNotBlank(requestDateStart), "delivery_date", requestDateStart)
                        .le(StringUtils.isNotBlank(requestDateEnd), "delivery_date", requestDateEnd)
                        .like(StringUtils.isNotBlank(purchGroup), "purch_group", purchGroup)
                        .like(StringUtils.isNotBlank(applier), "applier", applier)
                        .eq(StringUtils.isNotBlank(goodsStatus), "goods_status", goodsStatus)
                        .eq(StringUtils.isNotBlank(factory), "factory", factory)
                        .eq(StringUtils.isNotBlank(vendorCode), "vendor_code", vendorCode)
                        .eq(StringUtils.isNotBlank(materialCode), "material_code", materialCode)
                        .eq("delete_flag", "0")
                        .ne("purch_group","302")
                        .orderBy("purch_request_code", false));
        return new PageUtils(page);
    }

    @Override
    public List<ProPurchRequestItemEntity> findByNo(String purchRequestCode, List<String> ids) {
        return this.baseMapper.findByNo(purchRequestCode, ids);
    }

    @Override
    public ProPurchRequestItemEntity selectById(String pkId) {

        return this.baseMapper.selectById(pkId);
    }

    @Override
    public List<Map<String, String>> selectUserMapList(List<String> ids) {

        return this.baseMapper.selectUserMapList(ids);
    }

    @Override
    public List<ProPurchRequestItemEntity> selectBatchIds(List<String> ids) {
        return this.baseMapper.selectBatchIds(ids);
    }

    @Override
    public List<ProPurchRequestItemEntity> selectRequestItem() {

        return this.proPurchRequestItemDao.selectRequestItem();
    }

    //自定义分页
    @Override
    public PageUtils selectDetailPage(Map<String, Object> params) {
        Page<ProPurchRequestItemEntity> page = this.selectDetailPage(
                new QueryParam<ProPurchRequestItemEntity>(params).getPage(), new EntityWrapper<ProPurchRequestItemEntity>(),
                params);
        return new PageUtils(page);
    }

    private Page<ProPurchRequestItemEntity> selectDetailPage(Page<ProPurchRequestItemEntity> page,
                                                             Wrapper<ProPurchRequestItemEntity> wrapper, Map<String, Object> params) {
        page.setRecords(this.proPurchRequestItemDao.selectDetailPage(page, wrapper, params));
        return page;
    }

    @Override
    public boolean updateGoodsStatusBatch(List<ProPurchRequestItemEntity> list) {

        return this.baseMapper.updateGoodsStatusBatch(list);
    }

    @Override
    public boolean updateGoodsStatus(
            ProPurchRequestItemEntity proPurchRequestItemEntity) {

        return this.baseMapper.updateGoodsStatus(proPurchRequestItemEntity);
    }

    @Override
    public List<SsessAndProPurchRequest> selectSsess() {

        return this.baseMapper.selectSsess();
    }

    @Override
    public List<SsessAndProPurchRequest> selectSsessSimplify() {

        return this.baseMapper.selectSsessSimplify();
    }

    //货源分配从配额取则修改配额数
    @Override
    public boolean updateQuotaNum(EssentialInformationVO essentialInformationVO) {
        return proPurchRequestItemDao.updateQuotaNum(essentialInformationVO);
    }

    @Override
    public boolean updateProPurchRequestItemEntity(ProPurchRequestItemEntity proPurchRequestItemEntity) {
        return this.baseMapper.updateProPurchRequestItemEntity(proPurchRequestItemEntity);
    }

    //改变货源状态
    @Override
    public boolean changeGoodsStatus(int goodsStatus, String npkId) {
        ProPurchRequestItemEntity itemEntity = new ProPurchRequestItemEntity();
        itemEntity.setGoodsStatus(goodsStatus);
        itemEntity.setPkId(npkId);
        int row = this.baseMapper.updateById(itemEntity);
        return row > 0 ? true : false;
    }

    @Override
    public ProPurchRequestItemEntity findbypkId(String pkid) {
        return proPurchRequestItemDao.findbypkId(pkid);
    }

    @Override
    public void updateAll(ProPurchRequestItemEntity proPurchRequestItemEntity) {
        this.updateById(proPurchRequestItemEntity);
    }

    /**
     * 通过请购编码、请购行项目编码将请购单恢复草稿状态
     *
     * @param requestCode
     * @param purchRequestItemCode
     * @return
     */
    @Override
    public int updateStatus(String requestCode, String purchRequestItemCode) {
        return baseMapper.updateStatus(requestCode, purchRequestItemCode);
    }


}
